package com.leyou.item.service.impl;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class SpecificationServiceImpl implements SpecificationService {

    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;

    @Override
    public List<SpecGroup> queryGroupsByCid(Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        List<SpecGroup> specGroups = this.specGroupMapper.select(specGroup);
        if(CollectionUtils.isEmpty(specGroups)){
            throw new LyException(ExceptionEnum.SPECGROUP_NOT_FOUNT);
        }
        return specGroups;
    }

    @Override
    @Transactional
    public void updateSpecGroupName(SpecGroup specGroup) {
       int count = this.specGroupMapper.updateSpecGroupName(specGroup.getId(), specGroup.getName());
       if(count <= 0){
           throw new LyException(ExceptionEnum.SPECGROUPNAME_UPDATE_FAIL);
       }
    }

    @Override
    public void addSpecGroupName(Long cid, String name) {
        int count = this.specGroupMapper.addSpecGroupName(cid, name);
        if(count <= 0){
            throw new LyException(ExceptionEnum.SPECGROUPNAME_ADD_FAIL);
        }
    }

    @Override
    public void deleteSpecGroup(String id) {
        int count = this.specGroupMapper.deleteByPrimaryKey(id);
        if(count <= 0){
            throw new LyException(ExceptionEnum.SPECGROUPNAME_DELETE_FAIL);
        }
    }

    @Override
    public List<SpecParam> selectSpecParam(Long gid, Long cid, Boolean generic, Boolean searching) {
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setGeneric(generic);
        specParam.setSearching(searching);
        List<SpecParam> specParams = this.specParamMapper.select(specParam);
        if(CollectionUtils.isEmpty(specParams)){
            throw new LyException(ExceptionEnum.SPECPARAM_NOT_FOUNT);
        }
        return specParams;
    }

    @Override
    public void addSpecParam(SpecParam specParam) {
        if(specParam == null){
            throw new LyException(ExceptionEnum.PARAMETERS_ILLEGAL_OR_NULL);
        }
        int count = this.specParamMapper.insert(specParam);
        if(count <= 0){
            throw new LyException(ExceptionEnum.ADD_BRAND_FAIL);
        }
    }

    @Override
    public void updateSpecParam(SpecParam specParam) {
        if(specParam == null){
            throw new LyException(ExceptionEnum.PARAMETERS_ILLEGAL_OR_NULL);
        }
        int count = this.specParamMapper.updateByPrimaryKey(specParam);
        if(count <= 0){
            throw new LyException(ExceptionEnum.SPECPARAM_UPDATE_FAIL);
        }
    }

    @Override
    @Transactional
    public void deleteSpecParam(Long id) {
        if(id == null || id <= 0){
            throw new LyException(ExceptionEnum.PARAMETERS_ILLEGAL_OR_NULL);
        }
        SpecParam specParam = new SpecParam();
        specParam.setId(id);
        int count = this.specParamMapper.deleteByPrimaryKey(specParam);
        if(count <= 0){
            throw new LyException(ExceptionEnum.SPECPARAM_DELETE_FAIL);
        }
    }

    @Override
    public List<SpecGroup> querySpecsByCid(Long cid) {
        // 查询规格组
        List<SpecGroup> groups = this.queryGroupsByCid(cid);
        if(CollectionUtils.isEmpty(groups)){
            throw new LyException(ExceptionEnum.SPECGROUP_NOT_FOUNT);
        }
        groups.forEach(g -> {
            // 查询组内参数
            g.setParams(this.selectSpecParam(g.getId(), null, null, null));
        });
        return groups;
    }
}
